package com.sdhs.businessfile.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sdhs.businessfile.domain.BusinessFile;
import com.sdhs.businessfile.domain.dto.BusinessFileDTO;
import com.sdhs.businessfile.mapper.BusinessFileMapper;
import com.sdhs.businessfile.service.IBusinessFileService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;

/**
 * 业务附件Service业务层处理
 *
 * @author mz
 * @date 2024-07-30
 */
@Service
public class BusinessFileServiceImpl implements IBusinessFileService {
    @Autowired
    private BusinessFileMapper businessFileMapper;

    /**
     * 查询业务附件
     *
     * @param id 业务附件主键
     * @return 业务附件
     */
    @Override
    public BusinessFile selectBusinessFileById(Long id) {
        return businessFileMapper.selectBusinessFileById(id);
    }

    /**
     * 查询业务附件列表
     *
     * @param businessFile 业务附件
     * @return 业务附件
     */
    @Override
    public List<BusinessFile> selectBusinessFileList(BusinessFileDTO businessFile) {
        return businessFileMapper.selectBusinessFileList(businessFile);
    }

    /**
     * 根据业务类型和业务id查询列表
     *
     * @param businessType 业务类型
     * @param businessId 业务id
     *
     * @return 业务附件集合
     */
    @Override
    public List<BusinessFile> listByBusinessTypeAndId(String businessType, String businessId) {
        BusinessFileDTO businessFile = new BusinessFileDTO();
        businessFile.setBusinessType(businessType);
        businessFile.setBusinessId(businessId);
        return selectBusinessFileList(businessFile);
    }

    /**
     * 新增业务附件
     *
     * @param businessFileDTO 业务附件
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertBusinessFile(BusinessFileDTO businessFileDTO) {
        BusinessFile add = BeanUtil.toBean(businessFileDTO, BusinessFile.class);
        return businessFileMapper.insertBusinessFile(add);
    }

    /**
     * 修改业务附件
     *
     * @param businessFileDTO 业务附件
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateBusinessFile(BusinessFileDTO businessFileDTO) {
        BusinessFile update = BeanUtil.toBean(businessFileDTO, BusinessFile.class);
        return businessFileMapper.updateBusinessFile(update);
    }

    /**
     * 批量删除业务附件
     *
     * @param ids 需要删除的业务附件主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteBusinessFileByIds(Long[] ids) {
        return businessFileMapper.deleteBusinessFileByIds(ids);
    }

    /**
     * 删除业务附件信息
     *
     * @param id 业务附件主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteBusinessFileById(Long id) {
        return businessFileMapper.deleteBusinessFileById(id);
    }

    /**
     * 根据业务类型和业务id删除
     *
     * @param businessType 业务类型
     * @param businessId   业务id
     * @return 业务附件集合
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteByBusinessTypeAndId(String businessType, String businessId) {
        return businessFileMapper.deleteByBusinessTypeAndId(businessType, businessId) > 0;
    }

    /**
     * 根据业务类型和业务id删除
     *
     * @param businessType 业务类型
     * @param businessId   业务id
     * @param userName   用户名
     * @return 业务附件集合
     */
    @Override
    public Boolean deleteUserFileByBusinessTypeAndId(String businessType, String businessId, String userName) {
        return businessFileMapper.deleteUserFileByBusinessTypeAndId(businessType, businessId, userName) > 0;
    }

    /**
     * 批量保存
     * @param businessType 业务类型
     * @param businessId 业务id
     * @param fileList 文件列表
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveFileBatch(String businessType, String businessId, List<BusinessFile> fileList) {
        // 先删除
        this.deleteByBusinessTypeAndId(businessType,businessId);
        // 批量保存
        saveBatchCommon(businessType, businessId,fileList);
    }


    /**
     * 删除自己上传的附件，并重新添加新附件列表
     * @author wang
     * @date 2024/8/31 9:12
     * @param businessType 业务类型
     * @param businessId 业务id
     * @param fileList userName对应的用户上传的文件列表
     * @param userName 用户名
     * @return void
     */
    @Override
    public void saveUserFileBatch(String businessType, String businessId, List<BusinessFile> fileList, String userName) {
        // 先删除个人之前上传的数据
        deleteUserFileByBusinessTypeAndId(businessType, businessId, userName);
        // 批量保存
        saveBatchCommon(businessType, businessId,fileList);
    }

    public void saveBatchCommon(String businessType, String businessId, List<BusinessFile> fileList){
        // 上传新附件
        if (CollectionUtil.isNotEmpty(fileList)) {
            List<BusinessFile> list = new ArrayList<>();
            for (int i = 0; i < fileList.size(); i++) {
                BusinessFile add = fileList.get(i);
                add.setId(null);
                add.setBusinessType(businessType);
                add.setBusinessId(businessId);
                // 设置排序值：按照前端传递的顺序设置排序值
                add.setSort(i + 1);
                list.add(add);
            }
            businessFileMapper.insertBusinessFileBatch(list);
        }
    }

}
